package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

@Autowired
   private AttrMapper attrMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> queryGroupsWithAttrsByCid(Long cid) {
        //查询所有的分组
      List<AttrGroupEntity>  groupEntities=this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id",cid));
      if (CollectionUtils.isEmpty(groupEntities)){
          return groupEntities;
      }
        // 遍历分组查询组下的规格参数
        groupEntities.forEach(attrGroupEntity -> {
          List<AttrEntity> attrEntities= this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id",attrGroupEntity.getId()).eq("type",1));
         attrGroupEntity.setAttrEntities(attrEntities);
        });
        return groupEntities;
    }

    @Autowired
    private SpuAttrValueMapper baseAttrMapper;

    @Autowired
    private SkuAttrValueMapper saleAttrMapper;
    @Override
    public List<ItemGroupVo> queryGroupWithAttrValuesByCidAndSpuIdAndSkuId(Long cid, Long spuId, Long skuId) {
        // 根据cid查询分组
        List<AttrGroupEntity> groupEntities=this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id",cid));
        if (CollectionUtils.isEmpty(groupEntities)){
            return null;
        }
        // 把entity集合 转化成 vo集合
        return groupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo =new ItemGroupVo();
            itemGroupVo.setName(attrGroupEntity.getName());
            // 根据分组id查询组下的规格参数
            List<AttrEntity> attrEntities =  this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id",attrGroupEntity.getId()));
            if (CollectionUtils.isEmpty(attrEntities)){
                return itemGroupVo;
            }
            // 把规格参数集合 转化成 attrValueVo集合
            itemGroupVo.setAttrValues(attrEntities.stream().map(attrEntity -> {
                AttrValueVo attrValueVo =new AttrValueVo();
                attrValueVo.setAttrId(attrEntity.getId());
                attrValueVo.setAttrName(attrEntity.getName());
                if (attrEntity.getType()==1){
                    SpuAttrValueEntity spuAttrValueEntity = this.baseAttrMapper.selectOne(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id",spuId).eq("attr_id",attrEntity.getId()));
                  attrValueVo.setAttrValue(spuAttrValueEntity.getAttrValue());
                }else {
                    SkuAttrValueEntity skuAttrValueEntity = this.saleAttrMapper.selectOne(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id",skuId).eq("attr_id",attrEntity.getId()));
                   attrValueVo.setAttrValue(skuAttrValueEntity.getAttrValue());
                }
                return attrValueVo;
            }).collect(Collectors.toList()));
            return itemGroupVo;
        }).collect(Collectors.toList());
    }

}